package org.finesys.common.tenant.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.finesys.common.tenant.support.TenantContextHolder;
import org.springframework.core.Ordered;

@Aspect
public class IgnoreTenantAspect implements Ordered {

    @Around("@annotation(ignoreTenant)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint, IgnoreTenant ignoreTenant) throws Throwable {
        //租户配置原始信息
        Boolean ignore = TenantContextHolder.isIgnore();
        try {
            // 设置忽略租户上下文
            TenantContextHolder.setIgnore(true);
            // 执行逻辑
            return proceedingJoinPoint.proceed();
        } finally {
            // 恢复现场
            TenantContextHolder.setIgnore(ignore);
        }
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 1;
    }
}
